class BaddaysController < ApplicationController
  def create
    user = User.find_by_id(session[:user_id])
    category = Category.find_by_id(params[:category])
    @baddays = Badday.new
    badday_voter = BaddayVoter.new
    @baddays.message = params[:message]
    badday_voter.member_voter = 0
    badday_voter.remote_voter = '0'
    @baddays.agree = 0
    @baddays.disagree = 0
    @baddays.category = (category)
    if !params[:alias].nil?
      @baddays.alias = params[:alias]
    end
    user.baddays << (@baddays)
    if @baddays.valid?
      if @baddays.save
        @baddays.badday_voters << badday_voter
        flash[:badday] = "Badday message has been added"
      end
    end
    redirect_to :controller =>"workspaces", :action =>"badday"
  end
  def index
    @category = Category.all
    if !params[:cat].nil?
      @badday  = Badday.paginate(:all, :per_page => 10,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
    else
      @badday = Badday.paginate(:all, :per_page => 10,:page => params[:page])
    end
  end
end